﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Revis.VClinicNet.UI.External.Client.View;
using Revis.VClinicNet.UI.External.Client.Service;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Globalization;
using Revis.VClinicNet.Util.Translation;

namespace Revis.VClinicNet.UI.External.Client.ViewModel
{
    public class DoctorsViewModel : WorkspaceControlViewModelBase<Doctors>
    {
        public DoctorsViewModel()
        {
            _doctorsList = new ReadOnlyCollection<DoctorDto>(new List<DoctorDto>());
        }

        public override void Run()
        {
            Request();
        }

        public void Request()
        {
            ChangeState("Pobieranie informacji o lekarzach", RequestState.Requesting);
            _patientService.BeginDoctors(new AsyncCallback(RequestEnded), null);
        }

        public void RequestEnded(IAsyncResult result)
        {
            try {
                DoctorDto[] doctors = _patientService.EndDoctors(result);
                DoctorsList = new ReadOnlyCollection<DoctorDto>(doctors);
                ChangeState("Infomacje pobrane", RequestState.Success);
            } catch (Exception) {
                ChangeState("Wystąpił poważny błąd podczas pobierania listy lekarzy", RequestState.Faulted);
            }
        }

        private ReadOnlyCollection<DoctorDto> _doctorsList;
        public ReadOnlyCollection<DoctorDto> DoctorsList
        {
            get { return _doctorsList; }
            set
            {
                _doctorsList = value;
                RaisePropertyChanged("DoctorsList");
            }
        }
    }

    public class DayToPlDayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DayOfWeekTranslator.ToPLString((DayOfWeek)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
